﻿#region Copyright Notice
/*
 
DarkLakeTools, Dark Lake Software, Inc. (Provides Performance Enhancing Tools For ASP.NET applications)
Copyright (C) 2009  Dark Lake Software, Inc. 

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

Dark Lake Software, Inc. 
Postal Box 916461
Longwood, FL  32791
admin@darklakesoftware.com
 
*/
#endregion

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System;
using System.Text;
using System.Web.Caching;
using System.Configuration;
using DarkLakeTools.Configuration;

namespace DarkLakeTools.JSUtility
{
    public class JSUtility : IHttpHandler
    {
        #region IHttpHandler Members

        /// <summary>
        /// Gets a value indicating whether another request can use the <see cref="T:System.Web.IHttpHandler"/> instance.
        /// </summary>
        /// <value></value>
        /// <returns>true if the <see cref="T:System.Web.IHttpHandler"/> instance is reusable; otherwise, false.</returns>
        public bool IsReusable
        {
            get { return false; }
        }

        /// <summary>
        /// Enables processing of HTTP Web requests by a custom HttpHandler that implements the <see cref="T:System.Web.IHttpHandler"/> interface.
        /// </summary>
        /// <param name="context">An <see cref="T:System.Web.HttpContext"/> object that provides references to the intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests.</param>
        public void ProcessRequest(HttpContext context)
        {
            DarkLakeSection config = (DarkLakeSection)ConfigurationManager.GetSection("darkLakeSection"); 
            
            
            context.Response.Clear();
            
            //TODO: Add to config file
            context.Response.ContentType = "text/javascript"; // Used so that IIS7 will compress the file as well. 

            StringBuilder sb = new StringBuilder();

            if (context.Cache["DarkLakeTools.JSUtility"] != null)
            {
                sb.Append(context.Cache["DarkLakeTools.JSUtility"].ToString());
            }
            else
            {
                foreach (string file in System.IO.Directory.GetFiles(context.Server.MapPath(config.JSUtility.ScriptDirectory), "*.js"))
                {
                    sb.Append(System.IO.File.ReadAllText(file, Encoding.Default));
                }

                string finalJS; 
                
                if (config.JSUtility.EnableMinify)
                {

                    JSMin min = new JSMin();

                    finalJS = min.Minify(sb.ToString());
                }
                else
                {
                    finalJS = sb.ToString(); 
                }

                lock (context.Cache)
                {
                    context.Cache.Add("DarkLakeTools.JSUtility", finalJS, null, System.Web.Caching.Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Normal, null); 

                }
            }
            context.Response.Write(sb.ToString()); 
        }

        #endregion
    }
}
